package com.hontech.bv.ui.fragment.heat

import android.view.Gravity
import android.widget.Button
import com.hontech.bv.R
import com.hontech.bv.model.heat.HeatStatus
import com.hontech.bv.model.heat.Status
import com.hontech.bv.ui.AutoHideKeyActivity
import com.hontech.bv.ui.view.NumberSensorView
import com.hontech.bv.ui.window.MoveableWindow
import com.hontech.bv.utils.dimenIntById

private val width = dimenIntById(R.dimen.w600)
private val height = dimenIntById(R.dimen.h660)

private fun drawType(value: Int): String {
    return when (value) {
        0 -> "空闲"
        1 -> "抽水"
        2 -> "停止"
        else -> "未知"
    }
}

private fun heatType(value: Int): String {
    return when (value) {
        0 -> "空闲"
        1 -> "预加热"
        2 -> "异常"
        else -> "未知"
    }
}

class HeatSensor2Dialog : MoveableWindow(R.layout.dialog_heat_sensor2, width, height, false) {

    companion object {

        private var INSTANCE: HeatSensor2Dialog? = null

        fun show() {
            if (INSTANCE == null) {
                INSTANCE = HeatSensor2Dialog()
            }
            INSTANCE!!.show()
        }

        fun update(status: Status) {
            INSTANCE?.update(status)
        }
    }

    private val btnClose = view.findViewById<Button>(R.id.id_heat_sensor2_btn_close)

    private val numFlowCount = view.findViewById<NumberSensorView>(R.id.id_heat_sensor2_num_flow_count)
    private val numAdc = view.findViewById<NumberSensorView>(R.id.id_heat_sensor2_num_adc)
    private val numDraw = view.findViewById<NumberSensorView>(R.id.id_heat_sensor2_num_draw_state)
    private val numHeat = view.findViewById<NumberSensorView>(R.id.id_heat_sensor2_num_heat_state)

    init {
        btnClose.setOnClickListener {
            dismiss()
            INSTANCE = null
        }
    }

    fun show() {
        window.showAtLocation(AutoHideKeyActivity.parentView(), Gravity.CENTER, 0, 0)
        update(HeatStatus.status)
    }

    private fun update(status: Status) {
        numFlowCount.set(status.flowCount.value)
        numAdc.set(status.adc.value)
        numDraw.set(drawType(status.drawType.value))
        numHeat.set(heatType(status.heatType.value))
    }
}